/**
 *
 *
 *
 *
 *
 */

package org.sxp.modules.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.sxp.common.annotation.SysLog;
import org.sxp.common.utils.PageInfo;
import org.sxp.common.utils.PageResult;
import org.sxp.common.utils.Query;
import org.sxp.common.validator.ValidatorUtils;
import org.sxp.common.web.WrapperResponse;
import org.sxp.modules.sys.entity.SysConfigEntity;
import org.sxp.modules.sys.service.SysConfigService;

import java.util.List;

/**
 * 系统配置信息
 *
 *
 */
@RestController
@RequestMapping("/sys/config")
@Api(description="Config", tags = "系统配置信息表")
@SysLog(modules = "sys", value = "系统配置信息表")
public class SysConfigController extends AbstractController {
	@Autowired
	private SysConfigService sysConfigService;

	/**
	 * 列表
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name="pageNum", value = "当前页码", dataType="int", paramType = "header"),
			@ApiImplicitParam(name="pageSize",value="每页条目",dataType="int", paramType = "header"),
			@ApiImplicitParam(name="orderField",value="排序字段",dataType="string", paramType = "header"),
			@ApiImplicitParam(name="orderType",value="排序类型",dataType="string", paramType = "header", example = "asc或者desc")
	})
	@RequestMapping(value = "/list",method = {RequestMethod.POST,RequestMethod.GET})
	@ApiOperation(value = "查询列表")
	@RequiresPermissions("sys:config:list")
	public WrapperResponse<List<SysConfigEntity>> list(SysConfigEntity params) throws Exception{
		//请求头获取分页参数
		PageInfo pageInfo = buildPageInfo();
		IPage<SysConfigEntity> page = sysConfigService.page(
				new Query<SysConfigEntity>().getPage(pageInfo)
				,new LambdaQueryWrapper<SysConfigEntity>());

		return (WrapperResponse)WrapperResponse.success(new PageResult(page));
	}
	
	
	/**
	 * 配置信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:config:info")
	@ResponseBody
	public WrapperResponse<SysConfigEntity> info(@PathVariable("id") Long id){
		SysConfigEntity config = sysConfigService.getById(id);
		
		return WrapperResponse.success(config);
	}
	
	/**
	 * 保存配置
	 */
	@SysLog("保存配置")
	@RequestMapping("/save")
	@RequiresPermissions("sys:config:save")
	public WrapperResponse save(@RequestBody SysConfigEntity config){
		ValidatorUtils.validateEntity(config);

		sysConfigService.saveConfig(config);
		
		return WrapperResponse.success(null);
	}
	
	/**
	 * 修改配置
	 */
	@SysLog("修改配置")
	@RequestMapping("/update")
	@RequiresPermissions("sys:config:update")
	public WrapperResponse update(@RequestBody SysConfigEntity config){
		ValidatorUtils.validateEntity(config);
		
		sysConfigService.update(config);
		
		return WrapperResponse.success(null);
	}
	
	/**
	 * 删除配置
	 */
	@SysLog("删除配置")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:config:delete")
	public WrapperResponse delete(@RequestBody Long[] ids){
		sysConfigService.deleteBatch(ids);
		
		return WrapperResponse.success(null);
	}

}
